package cn.lblbc.puzzle

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.view.View
import androidx.appcompat.app.AppCompatActivity

abstract class ChoosePhotoActivity : AppCompatActivity() {
    private val CHOOSE_CODE = 3 // 只在相册挑选图片的请求码
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<View>(R.id.chooseButton).setOnClickListener { v: View? ->
            val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*")
            startActivityForResult(intent, CHOOSE_CODE)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
        super.onActivityResult(requestCode, resultCode, intent)
        if (resultCode == RESULT_OK && requestCode == CHOOSE_CODE) { // 从相册回来
            if (intent!!.data != null) {
                val uri = intent.data // 获得已选择照片的路径对象
                val bitmap = BitmapUtil.getAutoZoomImage(this, uri)
                onRefreshBitmap(bitmap)
            }
        }
    }

    abstract fun onRefreshBitmap(bitmap: Bitmap)
}